package com.situ.blog.dao.impl;

import com.situ.blog.dao.IUserDao;
import com.situ.blog.pojo.entity.User;
import com.situ.blog.pojo.query.UserQuery;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<User> selectByPage(UserQuery userQuery) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        // 存放查询参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String hql="from User where 1=1 ";
        if (!StringUtils.isEmpty(userQuery.getName())){
            hql+="and name like :name ";
            paramMap.put("name", "%"+userQuery.getName()+"%");
        }
        if (userQuery.getRole() != null) {
            hql+="and role = :role ";
            paramMap.put("role", userQuery.getRole());
        }
        // 起始索引
        int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
        Query query = session.createQuery(hql);
        // 设置查询参数
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, Object> param : paramMap.entrySet()) {
                query.setParameter(param.getKey(), param.getValue());
            }
        }
        // 从第几条记录开始查询
        query.setFirstResult(offset);
        // 一共查询多少条记录
        query.setMaxResults(userQuery.getLimit());
        return query.list();
    }

    @Override
    public Integer selectTotalCount(UserQuery userQuery) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        // 存放查询参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String hql="select count(*) from User where 1=1 ";
        if (!StringUtils.isEmpty(userQuery.getName())){
            hql+="and name like :name ";
            paramMap.put("name", "%"+userQuery.getName()+"%");
        }
        if (userQuery.getRole() != null) {
            hql+="and role = :role ";
            paramMap.put("role", userQuery.getRole());
        }
        // 起始索引
        int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
        Query query = session.createQuery(hql);
        // 设置查询参数
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, Object> param : paramMap.entrySet()) {
                query.setParameter(param.getKey(), param.getValue());
            }
        }
        // 获取总记录数
        List<?> countResult = query.list();
        int total = ((Number) countResult.get(0)).intValue();
        return total;
    }

    @Override
    public void deleteById(User user) {
        getHibernateTemplate().delete(user);
    }

    @Override
    public void add(User user) {
        getHibernateTemplate().save(user);
    }

    @Override
    public User login(String name, String password) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql = "from User where name = :name and password = :password";
        Query query = session.createQuery(hql);
        query.setParameter("name", name);
        query.setParameter("password", password);
        List<User> list = query.list();
        return list.get(0);
    }

    @Override
    public void updateStatus(int id, int status) {
    }
}
